package com.jhsw.roadsun.module.business.util;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.List;
import java.util.concurrent.*;

/**
 * @author Y.sugar
 * @since 2024/5/23 11:00
 */
public class PoolUtil {

    private static final ThreadPoolExecutor POOL = new ThreadPoolExecutor(
            8, 16888, 5,
            TimeUnit.MINUTES, new SynchronousQueue<>(),
            new BasicThreadFactory.Builder().namingPattern("pool-util-%d").daemon(false).build(),
            new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * 执行任务
     *
     * @param command 任务内容
     */
    public static void exec(Runnable command) {
        POOL.execute(command);
    }

    /**
     * 执行任务，返回期望值
     *
     * @param callable 任务内容
     * @return 任务期望执行的结果
     */
    public static <T> Future<T> exec(Callable<T> callable, Class<T> cls) {
        return POOL.submit(callable);
    }

    /**
     * 执行任务，返回期望值
     *
     * @param callable 任务内容
     * @return 任务期望执行的结果
     */
    public static <T> Future<List<T>> execList(Callable<List<T>> callable, Class<T> cls) {
        return POOL.submit(callable);
    }

    /**
     * 带返回结果的执行任务
     *
     * @param callable 任务内容
     * @return 任务执行结果
     */
    public static Future<Boolean> execBool(Callable<Boolean> callable) {
        return POOL.submit(callable);
    }
}
